package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.MapCarBrandBean;

import java.util.ArrayList;
import java.util.List;

public class X3ShowBrandAdapter extends RecyclerView.Adapter<X3ShowBrandAdapter.SelectBrandHolder> {

    private Context mContext;

    private List<MapCarBrandBean> mList;

    private List<Boolean> bList;

    public X3ShowBrandAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.bList = new ArrayList<>();
    }

    public void changeSelect(String select,List<MapCarBrandBean> mList) {
        this.mList = mList;
        this.bList.clear();
        for (int i = 0; i < mList.size(); i++) {
            bList.add(i,select.equals(mList.get(i).getName()));
        }
        notifyDataSetChanged();
    }

    public void changeReset() {
        this.bList.clear();
        for (int i = 0; i < mList.size(); i++) {
            bList.add(i,false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectBrandHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectBrandHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_brand_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectBrandHolder holder, int position) {

        holder.tv_brand.setText(mList.get(position).getName() + "");

        holder.tv_brand.setTextColor(bList.get(position) ? Color.parseColor("#0077ff") : Color.parseColor("#666666"));
        holder.tv_brand.setBackground(bList.get(position) ? mContext.getDrawable(R.drawable.x3_shape_idea_select_item) : mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0;i < bList.size(); i++) {
                    bList.remove(i);
                    bList.add(i,i == position);
                }
                notifyDataSetChanged();
                if (iOnClickSelect != null) {
                    iOnClickSelect.iOnClickSelectBrand(mList.get(position));
                }
            }
        });

    }

    IOnClickSelect iOnClickSelect;

    public void setiOnClickSelect(IOnClickSelect iOnClickSelect) {
        this.iOnClickSelect = iOnClickSelect;
    }

    public interface IOnClickSelect {
        void iOnClickSelectBrand(MapCarBrandBean carBrandBean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectBrandHolder extends RecyclerView.ViewHolder {
        TextView tv_brand;
        public SelectBrandHolder(@NonNull View itemView) {
            super(itemView);
            tv_brand = itemView.findViewById(R.id.tv_brand);
        }
    }
}
